package com.ethink.order.manager;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.id.generator.IdGenerator;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.order.api.enums.SaleOrderOrderStatusEnum;
import com.ethink.order.api.info.SaleDetailOrderInfo;
import com.ethink.order.api.request.SaleOrderAdd2Request;
import com.ethink.order.api.request.SaleOrderUpdateRequest;
import com.ethink.order.common.entity.SaleDetailOrder;
import com.ethink.order.common.entity.SaleOrder;
import com.ethink.order.common.mapper.SaleDetailOrderMapper;
import com.ethink.order.common.mapper.SaleOrderMapper;
import com.ethink.order.service.SaleDetailOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author default
 * @version 1.0.0
 * @ClassName SaleManager.java
 * @Description
 * @createTime 2022年11月22日 14:50:00
 */
@Component
public class SaleManager {

    @Autowired
    private SaleOrderMapper saleOrderMapper;

    @Autowired
    private SaleDetailOrderMapper saleDetailOrderMapper;

    @Autowired
    private SaleDetailOrderService saleDetailOrderService;

    @Autowired
    private IdGenerator idGenerator;

    @Transactional(rollbackFor = Exception.class)
    public Integer updateSaleOrder(SaleOrderUpdateRequest request) {
        SaleOrder order = saleOrderMapper.selectById(request.getId());
        if (!SaleOrderOrderStatusEnum.S1.eq(order.getOrderStatus())) {
            throw new BusinessException(OrderRespCode.SALE_ORDER_DETAIL_STATUS_IS_NOT_MET);
        }
        SaleOrder saleOrder = BeanUtil.copyProperties(request, SaleOrder.class, "item");
        saleDetailOrderMapper.delete(new LambdaQueryWrapper<SaleDetailOrder>()
                .eq(SaleDetailOrder::getOrderNo, order.getOrderNo()));
        saleDetailOrderService.batchInsert(request.getItem(), order.getOrderNo(),request.getExchangeRate());
        return saleOrderMapper.updateById(saleOrder);
    }


    @Transactional(rollbackFor = Exception.class)
    public void createFinish(List<SaleOrderAdd2Request> add2RequestList) {
        for (SaleOrderAdd2Request request : add2RequestList) {
            SaleOrder saleOrder = new SaleOrder();
            BeanUtils.copyProperties(request.getSaleOrderInfo(), saleOrder);
            saleOrder.setOrderStatus(SaleOrderOrderStatusEnum.S4.getIntValue());
            saleOrder.setId(null);
            saleOrder.setOrgId(null);
            saleOrder.setCreateTime(null);
            saleOrder.setUpdateTime(null);
            saleOrder.setIsDeleted(null);
            saleOrder.setCreateUser(null);
            saleOrder.setUpdateUser(null);
            saleOrder.setRemark(null);
            saleOrderMapper.insert(saleOrder);
            for (SaleDetailOrderInfo item : request.getItems()) {
                SaleDetailOrder detailOrder = new SaleDetailOrder();
                BeanUtils.copyProperties(item, detailOrder);
                detailOrder.setId(null);
                detailOrder.setOrgId(null);
                detailOrder.setCreateTime(null);
                detailOrder.setUpdateTime(null);
                detailOrder.setIsDeleted(null);
                detailOrder.setCreateUser(null);
                detailOrder.setUpdateUser(null);
                detailOrder.setRemark(null);
                saleDetailOrderMapper.insert(detailOrder);
            }
        }
    }
}
